/**
 * 设置抓拍机参数
 * @author xiaoxujs
 */

import { Form, Input, InputNumber, message, Modal } from "antd";
import { FC, useEffect, useState } from "react";
import { IYckzZyLoadControlParamSet, IYckzZyLoadControlParamSetZhuapaiji } from "../../../../../type/remoteControl/loadingControlList";
import { postYckzZyLoadControlCatchParamSet } from "../../../api";

const SetZPJModule: FC<{
  open: boolean,
  handleSetPlcCallback: () => void;
  clickData?: IYckzZyLoadControlParamSet,
  clickId?: string;
}> = ({
  open,
  handleSetPlcCallback,
  clickData,
  clickId
}) => {

    const [form] = Form.useForm<IYckzZyLoadControlParamSetZhuapaiji>();

    const [loading, setLoading] = useState(false);

    useEffect(() => {
      if (clickData) {
        form.setFieldsValue(clickData.setCatchVO)
      }
    }, [clickData, form])
    const handleOk = async () => {
      const values = await form.validateFields();
      if (clickData && clickId) {
        setLoading(true);
        const { code, msg } = await postYckzZyLoadControlCatchParamSet({
          ...values,
          catchId: clickData.setCatchVO.catchId,
          id: clickId
        }) as { code: number; msg: string };
        if (code === 20000) {
          form.resetFields();
          handleSetPlcCallback();
        } else {
          message.error(msg)
        }
        setLoading(false)

      }

    };

    const handleCancel = () => {
      handleSetPlcCallback();
    };
    return <Modal title="设置抓拍机参数" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
      <Form
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 14 }}
        form={form}
      >
        <Form.Item<IYckzZyLoadControlParamSetZhuapaiji>
          label="IP地址"
          name="catchIp"
          rules={[{ required: true, message: '请输入IP地址!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlParamSetZhuapaiji>
          label="端口号"
          name="catchPort"
          rules={[{ required: true, message: '请输入端口号!' }]}
        >
          <InputNumber style={{ width: '100%' }}  min={0} max={65535} />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlParamSetZhuapaiji>
          label="认证用户名"
          name="catchUser"
          rules={[{ required: true, message: '请输入认证用户名!' }]}
        >
          <Input />
        </Form.Item>
        <Form.Item<IYckzZyLoadControlParamSetZhuapaiji>
          label="认证用户密码"
          name="catchPassword"
          rules={[{ required: true, message: '请输入认证用户密码!' }]}
        >
          <Input />
        </Form.Item>
      </Form>
    </Modal>
  }

export default SetZPJModule;